package cn.tedu.ivos.application.service.impl;

import cn.tedu.ivos.application.mapper.ApplicationMapper;
import cn.tedu.ivos.application.pojo.dto.ApplicationQuery;
import cn.tedu.ivos.application.pojo.dto.ApplicationSaveParam;
import cn.tedu.ivos.application.pojo.entity.Application;
import cn.tedu.ivos.application.pojo.vo.ApplicationVo;
import cn.tedu.ivos.application.service.ApplicationService;
import cn.tedu.ivos.audit.pojo.vo.AuditVo;
import cn.tedu.ivos.audit.service.AuditService;
import cn.tedu.ivos.base.enums.ApplicationStatusEnum;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringJoiner;

@Slf4j
@Service
public class ApplicationServiceImpl implements ApplicationService {
    @Autowired
    ApplicationMapper applicationMapper;
    @Autowired
    AuditService auditService;
    @Autowired
    UserMapper userMapper;
    @Autowired
    VehicleMapper vehicleMapper;
    @Override
    public List<ApplicationVo> selects() {
        log.debug("查询所有申请单业务");
        List<ApplicationVo> list = applicationMapper.selects();
        return list;
    }

    @Override
    public void save(ApplicationSaveParam applicationSaveParam) {
        log.debug("保存申请单业务: {}",applicationSaveParam);
        Application application= new Application();
        BeanUtils.copyProperties(applicationSaveParam,application);
        application.setStatus(ApplicationStatusEnum.PENDING.getCode());
        application.setCreateTime(new Date());
        applicationMapper.save(application);
        auditService.insert(application);
        log.debug("保存申请单业务结束");
    }

    @Override
    public List<ApplicationVo> selectSearch(ApplicationQuery applicationQuery) {
        log.debug("查询申请单业务: {}",applicationQuery);
        List<ApplicationVo> list = applicationMapper.selectSearch(applicationQuery);
        for (int i = 0; i < list.size(); i++) {
            ApplicationVo applicationVo = list.get(i);
            assignAuditUserList(applicationVo);
        }
        log.debug("查询申请单业务结束: {}", list);
        return list;
    }
    private void assignAuditUserList(ApplicationVo applicationVo) {
        List<Long> auditUserIdList=new ArrayList<>();
        List<String> auditUserNameList=new ArrayList<>();
        List<AuditVo> auditVoList=auditService.selectAuditByApplicationId(applicationVo.getId());
        for (int i = 0; i <auditVoList.size() ; i++) {
            AuditVo auditVo = auditVoList.get(i);
            Long id = auditVo.getAuditUserId();
            auditUserIdList.add(id);
//            String userName = auditVo.getUserName();
            UserVO userVO = userMapper.selectAudit(id);
            auditUserNameList.add(userVO.getUsername());
        }
        StringJoiner stringJoiner=new StringJoiner(",");
        for (String username : auditUserNameList){
            stringJoiner.add(username);
        }
        applicationVo.setAuditUserIdList(auditUserIdList);
        applicationVo.setAuditUsernameList(stringJoiner.toString());
    }

    @Override
    public void cancle(Long id) {
        log.debug("撤销申请单业务: {}",id);
        Application application=new Application();
        application.setId(id);
        application.setStatus(ApplicationStatusEnum.CANCELED.getCode());
        application.setUpdateTime(new Date());
        applicationMapper.updateStatus(application);
        auditService.deleteAuditByApplicationId(id);
        log.debug("撤销申请单业务结束");
    }

    @Override
    public void distribut(Long applicationId, Long vehicleId) {
        log.debug("分配车辆业务: {},{}",applicationId,vehicleId);
        Application application=new Application();
        application.setId(applicationId);
        application.setVehicleId(vehicleId);
        application.setStatus(ApplicationStatusEnum.ALLOCATION.getCode());
        applicationMapper.updateStatus(application);
        Vehicle vehicle=new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setStatus("2");
        vehicle.setUpdateTime(new Date());
        vehicleMapper.update(vehicle);
    }
    @Override
    public void backCar(Long id, Long vehicleId) {
        log.debug("申请单退回业务: {},{}",id,vehicleId);
        Application application=new Application();
        application.setId(id);
        application.setStatus(ApplicationStatusEnum.PENDING.getCode());
        application.setUpdateTime(new Date());
        application.setVehicleId(null);
        applicationMapper.backCar(application);
        Vehicle vehicle=new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setStatus("1");
        vehicle.setUpdateTime(new Date());
        vehicleMapper.update(vehicle);
    }
}
